package org.csu.mypetstore.api.controller.front;


import com.alibaba.fastjson.JSONObject;
import com.zhenzi.sms.ZhenziSmsClient;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@Controller
@RequestMapping("/signon")
@SessionAttributes(value = {"account", "order", "cart","code","userInfo"})
public class SendCodeController {

      //短信平台相关参数
      //这个不用改
      private String apiUrl = "https://sms_developer.zhenzikj.com";
      //榛子云系统上获取
      private String appId = "111120";
      private String appSecret = "1184bfae-cfb8-40aa-b062-7e7dddbef9e9";

      @ResponseBody
      @RequestMapping("/sendCode")
      public boolean getCode(String memPhone, HttpServletRequest req , HttpSession httpSession, Model model){   //
            HttpSession session = req.getSession();
            try {
                  JSONObject json = null;
                  //随机生成验证码
                  String code = String.valueOf(new Random().nextInt(999999));
                  session.setAttribute("code",code);
                  //将验证码通过榛子云接口发送至手机
                  ZhenziSmsClient client = new ZhenziSmsClient(apiUrl, appId, appSecret);
                  Map<String, Object> params = new HashMap<String, Object>();
                  //前台输入的手机号

                  System.out.println(memPhone);
                  params.put("number",memPhone);

                  //这个模板id对应的是榛子云个人中心的模板id
                  params.put("templateId", 8509);
                  String[] templateParams = new String[2];
                  templateParams[0] = code;
                  templateParams[1] = "5分钟";
                  params.put("templateParams", templateParams);
                  String result = client.send(params);
                  System.out.println(result);
                  json = JSONObject.parseObject(result);

                  model.addAttribute("code",code);
                  req.getSession().setAttribute("code",code);

                  System.out.println(code);

                  if (json.getIntValue("code")!=0){//发送短信失败
                        return  false;
                  }
                  //将验证码存到session中,同时存入创建时间
                  //以json存放，这里使用的是阿里的fastjson
                  json = new JSONObject();
                  json.put("memPhone",memPhone);
                  json.put("code",code);
                  json.put("createTime",System.currentTimeMillis());
                  // 将认证码存入SESSION
                  httpSession.setAttribute("code",json);

                  return true;
            } catch (Exception e) {
                  e.printStackTrace();
                  return false;
            }
      }
}
